package com.bstech.CustomControls;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.bstech.Tabelle.Destinazioni;
import com.bstech.Tabelle.Destinazioni.Destinazione;
import com.bstech.assistance.R;

public class DestinazioniSelector extends Activity {

	private int idCliente;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.selector);
		
		int current = -1;
		Bundle bundle = this.getIntent().getExtras();
		if(bundle != null)
		{
			if(bundle.containsKey("idCliente"))
				idCliente = bundle.getInt("idCliente");
			if(bundle.containsKey("current"))
				current = bundle.getInt("current");
		}
		
		LoadControls(current);
	}
	
	private void LoadControls(int curretItemID)
	{
		ListView list = (ListView)findViewById(R.id.selector_listview);
		list.setEmptyView(findViewById(R.id.selector_emptyView));
		
		Destinazioni.DestinazioniAdapter adapter = new Destinazioni.DestinazioniAdapter(this, Destinazioni.SelectAll(idCliente));
		list.setAdapter(adapter);
		
		list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				Bundle b = new Bundle();
				b.putSerializable("selected", (Destinazione)arg0.getItemAtPosition(arg2) );
				Intent intent = new Intent();
				intent.putExtras(b);
				setResult(RESULT_OK, intent);
				finish();
			}
		});
		
		if(curretItemID > -1)
			list.setSelection(adapter.getItemPosition(curretItemID));
		
		adapter = null;
	}
}
